<template>
  <section  v-loading.lock="loading" element-loading-text="数据请求中" >
    <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="80px">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="12" :lg="4" :xl="4">
          <el-form-item label="登录账号" prop="legal_person">
            <el-input v-model="dataForm.username" type="text" placeholder="登录账号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="4" :xl="4">
          <el-form-item label="姓名" prop="legal_person">
            <el-input v-model="dataForm.real_name" type="text" placeholder="姓名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="4" :xl="4">
          <el-form-item label="注册时间" prop="sale">
            <el-date-picker
              v-model="dataForm.created_at"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :xs="20" :sm="20" :md="12" :lg="12" :xl="12">
          <el-form-item>
            <el-button type="primary" @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('member:import')" type="info" @click="downloadfile">数据导出</el-button>
            <el-button v-if="isAuth('member:import')" type="danger" @click="jumpTourl('member-import', 0)">批量导入</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      :row-class-name="rowClassName"
      v-loading="dataListLoading"
      @sort-change="changeSort"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        width="140"
        align="center"
        label="登录账号">
      </el-table-column>
      <el-table-column
        prop="member_company.0.title"
        header-align="center"
        align="center"
        label="企业名称">
        <template slot-scope="scope">
          <span v-for="item in scope.row.member_company" style="display: block;">{{item.title}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="member_info.real_name"
        header-align="center"
        align="center"
        width="120"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="member_info.id_card"
        header-align="center"
        align="center"
        width="190"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="member_info.bank_number"
        header-align="center"
        align="center"
        width="190"
        label="银行账号">
      </el-table-column>
      <el-table-column
        prop="member_info.alipay_account"
        header-align="center"
        align="center"
        width="190"
        label="支付宝账号">
      </el-table-column>
      <el-table-column
        prop="created_at"
        header-align="center"
        align="center"
        width="180"
        sortable="custom"
        sort-change="changeSort"
        label="注册时间">
      </el-table-column>
      <el-table-column
        prop="member_info.real_name_status"
        header-align="center"
        align="center"
        width="90"
        label="实名认证">
        <template slot-scope="scope">
          <span v-if="scope.row.member_info.real_name_status === -2" style="color: #F56C6C">认证失败</span>
          <span v-if="scope.row.member_info.real_name_status === -1" style="color: #F56C6C">未认证</span>
          <span v-else-if="scope.row.member_info.real_name_status === 1">已认证</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="member_info.real_name_status"
        header-align="center"
        align="center"
        width="90"
        label="协议状态">
        <template slot-scope="scope">
          <span v-if="scope.row.member_info.is_protocol === -1" style="color: #F56C6C">未签署</span>
          <span v-else-if="scope.row.member_info.is_protocol === 1">已签署</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="70"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === '禁用'" size="small" type="info">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="280"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.member_info.real_name_status == -1 && isAuth('member:replayAuth')" type="danger" size="small" @click="openReplayAuthDialog(scope.row)">重新认证</el-button>
          <el-button v-if="isAuth('member:details')" type="info" size="small" @click="jumpTourl('member-details', scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[15, 30, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="重新认证"
      :visible.sync="replayAuthVisible"
      width="600px">
      <el-form :model="formData" ref="form" label-width="100px">
        <el-form-item label="姓名" prop="">
          <el-input style="width: 400px;"v-model="formData.real_name"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="">
          <el-input style="width: 400px;"  v-model="formData.id_card"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="replayAuthVisible = false">取 消</el-button>
        <el-button type="primary" :loading="submitLoading" @click="submitData()">确 定</el-button>
      </span>
    </el-dialog>
  </section>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          username: '',
          real_name: '',
          created_at: '',
          sort_by: '',
          order_by: ''
        },
        formData: {
          member_id: '',
          real_name: '',
          id_card: ''
        },
        submitLoading: false,
        loading: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 15,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        replayAuthVisible: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    methods: {
      rowClassName({row, rowIndex}) {
        if(row.is_jg) {
          return 'bgred';
        }
        return ''
      },
      changeSort(val) {
        if (val.order !== null) {
          this.dataForm.sort_by = val.prop
          this.dataForm.order_by = val.order === 'ascending' ? 'asc' : 'desc'
        } else {
          this.dataForm.sort_by = ''
          this.dataForm.order_by = ''
        }
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      getCustomerList() {
        this.$http({
          url: this.$http.adornUrl('/member/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'username': this.dataForm.username,
            'real_name': this.dataForm.real_name,
            'legal_person': this.dataForm.legal_person,
            'created_at': this.dataForm.created_at,
            'sort': this.dataForm.sort_by,
            'order': this.dataForm.order_by
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      jumpTourl ( _url, _id) {
        this.$router.push({ name: _url, query: { id:  _id}})
      },
      downloadfile() {
        this.loading = true
        this.$http({
          url: this.$http.adornUrl('/member/download/list'),
          method: 'get',
          responseType: 'blob'
        }).then((res) => {
          let _data = res.data
          let blob = new Blob([_data])
          let reader = new FileReader()
          reader.readAsDataURL(blob)
          reader.onload = (e) => {
            let a = document.createElement('a')
            a.download = '用户数据信息.csv'
            a.href = e.target.result
            document.body.appendChild(a)
            a.click()
            a.remove()
          }
          this.loading = false;
        }).catch(() =>{
          this.loading = false;
        })
      },
      submitData() {
        this.submitLoading = true
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/replay/auth`),
              method: 'post',
              timeout: 1000 * 120,
              data: this.$http.adornData(this.formData)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '认证成功',
                  type: 'success',
                  duration: 1500
                })
                this.getDataList();
              } else {
                this.$message.error(data.msg)
              }
              this.submitLoading = false
            }).catch((error) => {
              this.submitLoading = false
              this.$message.error('系统处理中')
            })
          } else {
            this.submitLoading = false
          }
        })
      },
      openReplayAuthDialog(row) {
        this.replayAuthVisible = true
        console.log(row)
        this.formData = {
          member_id: row.id,
          real_name: row.member_info.real_name,
          id_card: row.member_info.id_card
        }
      }
    }
  }
</script>

<style lang="scss">
  .bgred {
    background-color: #fab6b6 !important;
  }
</style>
